﻿using MongoDB.Driver;
using Sustainalytics.Entities;
using Sustainalytics.ScreeningTool.Synchronization.Handlers;
using System;
using System.Collections.Generic;

namespace Sustainalytics.ScreeningTool.Synchronization.Logging
{
    public class HandlerLoggingDecorator<T> : IHandler<T> where T : IEntity
    {
        private readonly IHandler<T> _handler;

        public HandlerLoggingDecorator(IHandler<T> handler)
        {
            _handler = handler;
        }

        public IEnumerable<ExtraElement> GetExtraElements(T entity)
        {
            try
            {
                return _handler.GetExtraElements(entity);
            }
            catch (Exception ex)
            {
                var handlerName = _handler.GetType().Name;
                var companyId = entity.Id;
                Log.Error($"Exception thrown when getting extra elements using {handlerName} for company with id {companyId}", ex);
                throw;
            }
        }

        public ProjectionDefinition<T> GetProjection(ProjectionDefinitionBuilder<T> builder) => _handler.GetProjection(builder);
    }
}
